#ifndef am_am_Extract_h
#define am_am_Extract_h



#include <string>
#include <tbb\concurrent_vector.h>

#include <math.h>
#include <fstream>

#include "..\AuroraTypes.hpp"
#include <borealis\Raster.hpp>
#include <borealis\AsciiRaster.hpp>
#include <borealis\Interpolation.hpp>

namespace Aurora
{

	/*
	Class: am_ExtractValues
	
	*/
	class am_Extract : public AuroraTypes::ModuleBase
	{
		//in vars
		int GRID;

		// out vars
		int FILE;
		std::string m_interpMethod;

	public:

		am_Extract(std::string n)
		{
			GRID		=0;

			FILE		=0;

			name = n;
		}



		void run(AuroraTypes::StationList* stations,AuroraTypes::VariableList* variables, AuroraTypes::InVariables& inVars, AuroraTypes::OutVariables& outVars)
		{
			boost::shared_ptr<Borealis::Raster<float> > in;
			std::ofstream out;
			std::string file = outVars[0];

			out.open(file.c_str(),std::ios::app);
			if(!out.is_open())
				throw std::runtime_error("In " + std::string(__FILE__) +" @ line " + boost::lexical_cast<std::string>(__LINE__) +" Unable to open files for append");

			//bail if we don't have any met stations
			if(stations->size() == 0)
				throw std::runtime_error("In " + std::string(__FILE__) +" @ line " + boost::lexical_cast<std::string>(__LINE__) +"No met data");

			try
			{
				in = GetRaster(variables,inVars[GRID]);

			}
			catch (std::runtime_error e)
			{
				throw std::runtime_error("In " + std::string(__FILE__) +" @ line " + boost::lexical_cast<std::string>(__LINE__) +" - " + std::string(e.what()));

			}


			//locations

			//vista view
			unsigned int vvx = (628332.0 - in->GetXllCorner())/in->GetCellSize();
			unsigned int vvy = in->GetRow() - (5648184.0 - in->GetYllCorner())/in->GetCellSize(); //have to subtract the row other wise we get a mirror image, as we index from top left instead of bottom left
			vvy--; //need to do this as we start index at 0

			//upper clearing
			unsigned int ucx = (628150.0 - in->GetXllCorner())/in->GetCellSize();
			unsigned int ucy = in->GetRow() - (5646577.0 - in->GetYllCorner())/in->GetCellSize(); //have to subtract the row other wise we get a mirror image, as we index from top left instead of bottom left
			ucy--; //need to do this as we start index at 0

			//alpine/nakiska ridge
			unsigned int nrx = (626896.0 - in->GetXllCorner())/in->GetCellSize();
			unsigned int nry = in->GetRow() - (5645231.0 - in->GetYllCorner())/in->GetCellSize(); //have to subtract the row other wise we get a mirror image, as we index from top left instead of bottom left
			nry--; //need to do this as we start index at 0

			//haymeadow
			unsigned int hmx = (630742.0 - in->GetXllCorner())/in->GetCellSize();
			unsigned int hmy = in->GetRow() - (5645259.0 - in->GetYllCorner())/in->GetCellSize(); //have to subtract the row other wise we get a mirror image, as we index from top left instead of bottom left
			hmy--; //need to do this as we start index at 0

			float vvValue = in->GetRasterAt(vvy,vvx);
			float ucValue = in->GetRasterAt(ucy,ucx);
			float nrValue = in->GetRasterAt(nry,nrx);
			float hmValue = in->GetRasterAt(hmy,hmx);

			out << vvValue << "\t" << ucValue << "\t" << nrValue << "\t" << hmValue << std::endl;

			out.close();

		}


	};
}

#endif